#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define mod 998244353
#define N 150010
#define int long long
#define szi sizeof(int)
#define il inline
int T,X[4],n,m,k,l,_a[N],a[N],q[N],f[18][N],w[18][N],ans,d[18][N],mx;
char st[N];
il bool chk(int p){
    for(int i=1;i<=l;++i){
        if(p&1){
            if(f[i-1][p+1]>=i&&d[i][(p+1)>>1]) return false; p=(p+1)>>1;
        }
        else{
            if(f[i-1][p-1]>=i&&!d[i][p>>1]) return false; p>>=1;
        }
    }
    return true;
}
il int calc(int nw){
    int res=0; l=0; while((1<<l)<nw) ++l;
    if((1<<l)==nw){
        if(mx>=l) return w[l][1]; mx=max(mx,l);
    }
    memcpy(f[0],a,szi*(nw+1));
    for(int i=nw+1;i<=(1<<l);++i) f[0][i]=0;
    for(int i=1;i<=(1<<l);++i) w[0][i]=(i<=nw)?i:0;
    for(int i=1;i<=l;++i)
        for(int j=2;j<=(1<<l-i+1);j+=2){
            if(d[i][j>>1]){
                if(f[i-1][j]>=i) f[i][j>>1]=f[i-1][j],w[i][j>>1]=w[i-1][j];
                else f[i][j>>1]=f[i-1][j-1],w[i][j>>1]=w[i-1][j-1];
            }
            else{
                if(f[i-1][j-1]>=i) f[i][j>>1]=f[i-1][j-1],w[i][j>>1]=w[i-1][j-1];
                else f[i][j>>1]=f[i-1][j],w[i][j>>1]=w[i-1][j];
            }
        }
    for(int i=nw+1;i<=(1<<l);++i) if(chk(i)) res+=i;
    return w[l][1]+res;
}
il void solve(){
    scanf("%lld%lld%lld%lld",&X[0],&X[1],&X[2],&X[3]);
    ans=0,mx=-1;
    for(int i=1;i<=n;++i) a[i]=_a[i]^X[i%4];
    for(int i=1;i<=m;++i) ans^=(i*calc(q[i]));
    printf("%lld\n",ans);
    return ;
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%lld%lld",&n,&m),k=0;
    for(int i=1;i<=n;++i) scanf("%lld",&_a[i]);
    for(int i=1;i<=m;++i) scanf("%lld",&q[i]);
    while((1<<k)<n) ++k;
    for(int i=1;i<=k;++i){
        scanf("%s",st+1);
        for(int j=1;j<=(1<<k-i);++j) d[i][j]=st[j]-'0';
    }
    scanf("%lld",&T); while(T--) solve();
    return 0;
}
